﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Api.Common;
using AppManager.Model;
using AppManager.DomainModel;
using AppManager.IBLL;

namespace AppManager.Background
{
    public class UserController : BaseController
    {
        IUserService userService;
        IAppInfoService appInfoService;
        public UserController(IUserService userServ, IAppInfoService appInfoServ)
        {
            userService = userServ;
            appInfoService = appInfoServ;
        }
        // GET: App
        public ActionResult Create()
        {
            ViewBag.TableTitle = "用戶添加";
            Th.PageHeaderCrumbs = new List<string> { "用戶添加", "Home", "用戶管理" };
            Th.PageHeaderMenus = new Dictionary<string, string> { { "用戶列表", Url.Action("List") } };

            return View("Edit");
        }

        public ActionResult Edit(int? id)
        {
            ViewBag.TableTitle = "用戶修改";
            Th.PageHeaderCrumbs = new List<string> { "用戶修改", "Home", "用戶管理" };
            Th.PageHeaderMenus = new Dictionary<string, string> { { "用戶列表", Url.Action("List") } };
            try
            {
                return View(userService.GetModel(id));
            }
            catch (MsgEx ex)
            {
                AddErrorInfo(ex.Message);
            }
            return View();
        }

        /// <summary>
        ///     只能修改用戶名和登錄Id
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Edit(Sys_UserEidtModel model)
        {
            if (model.Id > 0)
            {
                ViewBag.TableTitle = "用戶修改";
                Th.PageHeaderCrumbs = new List<string> { "用戶修改", "Home", "用戶管理" };
            }
            else
            {
                ViewBag.TableTitle = "用戶添加";
                Th.PageHeaderCrumbs = new List<string> { "用戶添加", "Home", "用戶管理" };
            }

            Th.PageHeaderMenus = new Dictionary<string, string> { { "用戶列表", Url.Action("List") } };

            #region 只负责验证
            //移除這三個屬性的驗證
            ModelState.Remove("Id");
            //如果修改時，不需要檢測某些屬性
            if (model.Id > 0)
            {
                ModelState.Remove("LoginPwd");
                ModelState.Remove("LoginPwd2");
            }

            if (!ModelState.IsValid)
            {
                ViewBag.HasModelError = true;
                return View(model);
            }
            #endregion

            try
            {
                var id = userService.AddOrUpdateUser(model);
                if (id > 0)
                {
                    return RedirectToAction("List");
                }
                AddErrorInfo("操作失敗");
            }
            catch (Exception ex)
            {
                AddErrorInfo(ex.Message);
            }
            return View(model);
        }

        public ActionResult List()
        {
            ViewBag.TableTitle = "用戶列表";
            Th.PageHeaderCrumbs = new List<string> { "用戶列表", "Home", "用戶管理" };
            Th.PageHeaderMenus = new Dictionary<string, string> { { "用戶添加", Url.Action("Create") } };
            var appList = userService.GetList();

            var ids = appList.Select(s => s.Id);
            var infos = appInfoService.GetList(s => ids.Contains(s.AppId));

            var columns = new List<string> { "登錄Id", "用戶名", "操作" };
            ViewBag.Columns = columns;

            return View(appList);
        }

        /// <summary>
        ///     多选删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(params int[] ids)
        {
            //如果有的話就
            if (ids == null || ids.Count() == 0)
            {
                return Json(new AjaxResult { status = false, msg = "請選擇一項" });
            }

            userService.DeleteList(s => ids.Contains(s.Id));
            return Json(new AjaxResult { msg = "操作成功", url = Url.Action("List") });
        }
        /// <summary>
        /// 点击某项的删除链接删除后跳转到list页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int? id)
        {
            //如果有的話就
            if (id == null)
            {
                return RedirectToAction("List");
            }

            userService.Delete(s => s.Id == id.Value);
            return RedirectToAction("List");
        }
    }
}